package com.cskaoyan.service.mall;

import com.cskaoyan.bean.po.ohter.MarketKeyword;
import com.cskaoyan.bean.po.ohter.MarketKeywordExample;
import com.cskaoyan.bean.vo.mall.KeywordListInfo;
import com.cskaoyan.bean.vo.mall.KeywordListVo;
import com.cskaoyan.mapper.MarketKeywordMapper;
import com.cskaoyan.util.beanutils.BeanUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Author Ajun de BBC
 * @Date 2022/7/17 13:09
 * @Version 1.5
 */

@Service
public class KeywordServiceImpl implements KeywordService {
    @Autowired
    MarketKeywordMapper marketKeywordMapper;

    /**
     *@Description :关键词的页面显示以及模糊查询
     *@Param :
     *@Return :
     */
    @Override
    public KeywordListVo keywordList(Integer page, Integer limit, String keyword, String url, String sort, String order) {
        PageHelper.startPage(page,limit);//开启分页
        MarketKeywordExample marketKeywordExample = new MarketKeywordExample();
        marketKeywordExample.setOrderByClause(sort + " " + order);
        MarketKeywordExample.Criteria criteria = marketKeywordExample.createCriteria();//动态拼接sql语句
        keyword = (keyword == null) ? "%" : "%" + keyword + "%";
        url = ( url == null ) ? "%" : "%" + url + "%";
        criteria.andKeywordLike(keyword);
        criteria.andUrlLike(url);
        criteria.andDeletedEqualTo(false);
        List<MarketKeyword> marketKeywords = marketKeywordMapper.selectByExample(marketKeywordExample);

        List<KeywordListInfo> keywordListInfos = new ArrayList<>();
        for (MarketKeyword marketKeyword : marketKeywords) {
            KeywordListInfo keywordListInfo = new KeywordListInfo();
            BeanUtil.copyBean(marketKeyword,keywordListInfo);
            keywordListInfos.add(keywordListInfo);
        }

        PageInfo<MarketKeyword> marketKeywordPageInfo = new PageInfo<>(marketKeywords);
        Long total = marketKeywordPageInfo.getTotal();
        int pages = marketKeywordPageInfo.getPages();

        return new KeywordListVo(total.intValue(),pages,limit,page,keywordListInfos);
    }

    /**
     *@Description : 关键词的增加
     *@Param :
     *@Return :
     */
    @Override
    public KeywordListInfo keywordCreate(KeywordListInfo info) {
        info.setAddTime(new Date(System.currentTimeMillis()));
        info.setUpdateTime(new Date(System.currentTimeMillis()));
        marketKeywordMapper.insertOneKeyword(info);
        return info;
    }

    /**
     *@Description : 关键词的修改
     *@Param :
     *@Return :
     */
    @Override
    public void keywordUpdate(KeywordListInfo info) {
        info.setUpdateTime(new Date(System.currentTimeMillis()));
        info.setSortOrder(100);
        MarketKeyword marketKeyword = new MarketKeyword();
        BeanUtil.copyBean(info,marketKeyword);
        marketKeywordMapper.updateByPrimaryKey(marketKeyword);

    }

    /**
     *@Description : 关键词的假性删除 True(1)表示假性删除，不显示在页面，False(0)正常显示在页面
     *@Param :
     *@Return :
     */
    @Override
    public void keywordDelete(KeywordListInfo info) {
        info.setSortOrder(100);
        info.setDeleted(true);
        MarketKeyword marketKeyword = new MarketKeyword();
        BeanUtil.copyBean(info,marketKeyword);
        marketKeywordMapper.updateByPrimaryKey(marketKeyword);

    }
}
